package com.lx.boot;


import com.lx.boot.web.AuthUtil;
import com.lx.boot.web.SecurityFilter;
import com.lx.constant.DefaultBaseConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@AutoConfiguration
@ComponentScan(basePackages = {"com.lx.boot"})
public class BootAutoConfiguration {


    @Bean
    @ConditionalOnMissingBean(AuthUtil.class)
    public AuthUtil authUtil(){
        return new AuthUtil();
    }

    @Bean
    @ConditionalOnProperty(name = DefaultBaseConstant.SECURITY_FILTER_ENABLE, havingValue = "true", matchIfMissing = true)
    public FilterRegistrationBean pubSecurityFilter(@Autowired AuthUtil authUtil) {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new SecurityFilter(authUtil));
        //配置过滤规则
        filterRegistrationBean.addUrlPatterns("*");
        //设置过滤器名称
        filterRegistrationBean.setName("securityFilter");
        //执行次序,值越小，则越早执行
        filterRegistrationBean.setOrder(Integer.MIN_VALUE+10);

        return filterRegistrationBean;
    }

    @Bean("executor")
    public Executor executor(){
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(10);
        taskExecutor.setMaxPoolSize(50);
        taskExecutor.setQueueCapacity(200);
        taskExecutor.setKeepAliveSeconds(60);
        taskExecutor.setThreadNamePrefix("taskExecutor--");
        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        taskExecutor.setAwaitTerminationSeconds(60);
        return taskExecutor;
    }


}
